package gui.gui2d;

import gui.GuiManager;
import gui.gui2d.eventsmanager.MouseListenerSelecterPiece;
import gui.gui2d.highLevelPanels.ChatPanel;
import gui.gui2d.highLevelPanels.HistoryMovedPanel;
import gui.gui2d.highLevelPanels.InfoPanel;
import gui.gui2d.highLevelPanels.MenuPanel;
import gui.gui2d.highLevelPanels.OpponentEatenListPanel;
import gui.gui2d.highLevelPanels.PlayerEatenListPanel;
import gui.gui2d.highLevelPanels.SelecterPiecePanel;
import gui.gui2d.highLevelPanels.utils.Animation;
import gui.gui2d.highLevelPanels.utils.SelecterExitInfoPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import utility.PieceImage;
import utility.Point;
import world.player.Player;

public class GamePanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int WIDTH_EATEN_LIST_PANEL = 200;
	private static final int HEIGHT_MENU_PANEL = 100;
	private static final int HEIGHT_INFO_PANEL = 400;
	private static int FULL_HD_X=1920;
	private static int FULL_HD_Y=1080;
	private static LayoutManager DEFAULT_LAYER = new BorderLayout(1,1);
	private JPanel mainRightPanel;
	private JPanel mainLeftPanel;
	private JPanel secondaryRight_MainRightPanel;
	private JPanel secondaryBottom_MainRightPanel;
	private JPanel secondaryLeftMainLeftPanel;
	private JPanel loadingPAnel;
	int percentDimentionOfMainRightPanel;//defined in setStyle
	int percentDimentionOfMainBottomPanel;//defined in setStyle
	MouseListenerSelecterPiece mlsp ;
	Animation a ;
	
	int spaceFromBottomPlayerEatenListPanel=0;
	int spaceTopOpponentEatenListPanel=0;
	
	JScrollPane jsbHistoryMoved ;

	
	/*
	 * Main panel to work
	 */
	private JPanel mainCenterPanel;
	
	private JPanel opponentEatenListPanel;
	private JPanel playerEatenListPanel;
	
	private JPanel menuPanel;
	private JPanel historyMovedPanel;
	private JPanel infoPanel;
	
	private JPanel mainTopPanel;
	private JPanel chatPanel;
	
	private JPanel chessBoardPanel;
	
	private HistoryMovedPanel hmp;
	
	private InfoPanel ip;
	
	private ChatPanel cp;
	
	private PlayerEatenListPanel pelp;
	private OpponentEatenListPanel oelp;
	
	private ChessBoardPanel cbp;
	
	private SelecterPiecePanel selecterPiecePanel;
	private SelecterExitInfoPanel selecterExitInfoPanel;
	
	
	public GamePanel(String ID) {
		chessBoardPanel=new JPanel();
		mainTopPanel= 		new JPanel();
		mainCenterPanel= 	new JPanel();
		chatPanel= 	new JPanel();
		mainRightPanel= 	new JPanel();
		mainLeftPanel= 	new JPanel();
		playerEatenListPanel= new JPanel();
		secondaryRight_MainRightPanel= new JPanel();
		menuPanel= new JPanel();
		secondaryBottom_MainRightPanel=new JPanel();
		historyMovedPanel= new JPanel();
		infoPanel= new JPanel();
		secondaryLeftMainLeftPanel= new JPanel();
		opponentEatenListPanel=new JPanel();
		loadingPAnel = new JPanel();
		secondaryLeftMainLeftPanel.setLayout(new BorderLayout());
		loadingPAnel.setOpaque(false);
		this.setLayout(DEFAULT_LAYER);
		//this.add(topPanel, BorderLayout.NORTH);
		this.add(mainCenterPanel, BorderLayout.CENTER);
		this.add(mainRightPanel, BorderLayout.EAST);
		//this.add(bottomPanel, BorderLayout.SOUTH);
		this.add(mainLeftPanel, BorderLayout.WEST);
		mainCenterPanel.setLayout(new BorderLayout());
		mainCenterPanel.add(chatPanel,BorderLayout.SOUTH);
		mainCenterPanel.add(mainTopPanel,BorderLayout.NORTH);
		mainCenterPanel.add(chessBoardPanel,BorderLayout.CENTER);
		mainRightPanel.setLayout(new BorderLayout());
		mainRightPanel.add(playerEatenListPanel,BorderLayout.WEST);
		mainRightPanel.add(secondaryRight_MainRightPanel,BorderLayout.CENTER);
		secondaryRight_MainRightPanel.setLayout(new BorderLayout(1,1));
		secondaryRight_MainRightPanel.add(menuPanel,BorderLayout.NORTH);
		secondaryRight_MainRightPanel.add(secondaryBottom_MainRightPanel,BorderLayout.CENTER);
		secondaryBottom_MainRightPanel.setLayout(new BorderLayout());
		secondaryBottom_MainRightPanel.add(historyMovedPanel,BorderLayout.CENTER);
		secondaryBottom_MainRightPanel.add(infoPanel,BorderLayout.NORTH);
		mainLeftPanel.setLayout(new BorderLayout());
		mainLeftPanel.add(secondaryLeftMainLeftPanel, BorderLayout.CENTER);
		mainLeftPanel.add(opponentEatenListPanel, BorderLayout.EAST);
		setStyle(ID);
		setTramsparence();
		selecterExitInfoPanel= new SelecterExitInfoPanel();
		selecterExitInfoPanel.setOpaque(false);
		infoPanel.add(selecterExitInfoPanel,BorderLayout.CENTER);
	}
	
	void setTramsparence(){
		//this.setBackground(new Color(0,0,0,0));
		this.setOpaque(false);
		
		//mainTopPanel.setBackground(new Color(0,0,0,0));
		mainTopPanel.setOpaque(false);
		
		//mainRightPanel.setBackground(new Color(0,0,0,0));
		mainRightPanel.setOpaque(false);
		
		//mainLeftPanel.setBackground(new Color(0,0,0,0));
		mainLeftPanel.setOpaque(false);
		
		//chatPanel.setBackground(new Color(0,0,0,0));
		chatPanel.setOpaque(false);
		
		//chessBoardPanel.setBackground(new Color(0,0,0,0));
		chessBoardPanel.setOpaque(false);
		
		//opponentEatenListPanel.setBackground(new Color(0,0,0,0));
		opponentEatenListPanel.setOpaque(false);
		
		//playerEatenListPanel.setBackground(new Color(0,0,0,0));
		playerEatenListPanel.setOpaque(false);
		
		//secondaryLeftMainLeftPanel.setBackground(new Color(0,0,0,0));
		secondaryLeftMainLeftPanel.setOpaque(false);
		
		//secondaryRight_MainRightPanel.setBackground(new Color(0,0,0,0));
		secondaryRight_MainRightPanel.setOpaque(false);
		
		//secondaryBottom_MainRightPanel.setBackground(new Color(0,0,0,0));
		secondaryBottom_MainRightPanel.setOpaque(false);
		
		//infoPanel.setBackground(new Color(0,0,0,20));
		infoPanel.setOpaque(false);
		
		//menuPanel.setBackground(new Color(0,0,0,10));
		menuPanel.setOpaque(false);
		
		//historyMovedPanel.setBackground(new Color(0,0,0,20));
		historyMovedPanel.setOpaque(false);
		
		mainCenterPanel.setOpaque(false);
	}
	
	
	private void setStyle(String ID) {
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		if(screenSize.width<FULL_HD_X){
			percentDimentionOfMainRightPanel=70;
			
			if(ID.equals(GuiManager.LOCAL_GAME)){
				percentDimentionOfMainBottomPanel=50;
			}else if(ID.equals(GuiManager.LAN_GAME)){
				percentDimentionOfMainBottomPanel=90;
			}
		}else{
			percentDimentionOfMainRightPanel=50;
			if(ID.equals(GuiManager.LOCAL_GAME)){
				percentDimentionOfMainBottomPanel=50;
			}else if(ID.equals(GuiManager.LAN_GAME)){
				percentDimentionOfMainBottomPanel=95;
			}
		}
		int pixelReservedChessBoard=850;
		int xDimention=screenSize.width;
		int xWorkingWindow=xDimention-pixelReservedChessBoard;
		int dimentionMainRightPanel=(xWorkingWindow*percentDimentionOfMainRightPanel)/100;
		int dimentionMainLeftPanel=xWorkingWindow-dimentionMainRightPanel;
		int yDimention=screenSize.height;
		int yWorkingWindow=yDimention-pixelReservedChessBoard;
		int dimentionMainBottomPanel=(yWorkingWindow*percentDimentionOfMainBottomPanel)/100;
		int dimentionMainTopPanel=yWorkingWindow-dimentionMainBottomPanel;
		mainLeftPanel.setPreferredSize(new Dimension(dimentionMainLeftPanel, 0));
		mainRightPanel.setPreferredSize(new Dimension(dimentionMainRightPanel, 0));
		mainTopPanel.setPreferredSize(new Dimension(0, dimentionMainTopPanel));
		chatPanel.setPreferredSize(new Dimension(0,dimentionMainBottomPanel));
		playerEatenListPanel.setPreferredSize(new Dimension(WIDTH_EATEN_LIST_PANEL,0));
		menuPanel.setPreferredSize(new Dimension(0,HEIGHT_MENU_PANEL));
		infoPanel.setPreferredSize(new Dimension(0,HEIGHT_INFO_PANEL));
		opponentEatenListPanel.setPreferredSize(new Dimension(WIDTH_EATEN_LIST_PANEL,0));
		if(ID.equals(GuiManager.LAN_GAME)){
			setSpaceFromBottomPlayerEatenListPanel(170);
			setSpaceTopOpponentEatenListPanel(20);
		}else{
			setSpaceFromBottomPlayerEatenListPanel(130);
			setSpaceTopOpponentEatenListPanel(130);
		}
	}
	
	public void addPlayerEatenListPanel(PlayerEatenListPanel playerEatenListPanel){
		
		this.playerEatenListPanel.setLayout(new BorderLayout());
		JPanel northPanel= new JPanel();
		northPanel.setLayout(new BorderLayout());
		northPanel.add(playerEatenListPanel,BorderLayout.SOUTH);
		JPanel southPanel= new JPanel();
		southPanel.setOpaque(false);
		northPanel.setOpaque(false);
		this.playerEatenListPanel.add(northPanel, BorderLayout.CENTER);
		this.playerEatenListPanel.add(southPanel, BorderLayout.SOUTH);
		southPanel.setPreferredSize(new Dimension(0,getSpaceFromBottomPlayerEatenListPanel()));
		this.pelp=playerEatenListPanel;
		this.repaint();
	}
	
	public void addOpponentEatenListPanel(OpponentEatenListPanel opponentEatenListPanel){
		JPanel allPanels= new JPanel();
		allPanels.setOpaque(false);
		JPanel northPanel= new JPanel();
		northPanel.setOpaque(false);
		
		northPanel.setPreferredSize(new Dimension(0,getSpaceTopOpponentEatenListPanel()));
		JPanel soutJPanel= new JPanel();
		soutJPanel.setOpaque(false);
		allPanels.setLayout(new BorderLayout());
		allPanels.add(northPanel , BorderLayout.NORTH);
		allPanels.add(soutJPanel,BorderLayout.CENTER);
		soutJPanel.setLayout(new BorderLayout());
		this.opponentEatenListPanel.setLayout(new BorderLayout());
		soutJPanel.add(opponentEatenListPanel);
		this.opponentEatenListPanel.add(allPanels, BorderLayout.NORTH);
		this.oelp=opponentEatenListPanel;
		this.repaint();
	}
	public void addMenuPanel(MenuPanel menuPanel){
		this.menuPanel.setLayout(new BorderLayout());
		this.menuPanel.add(menuPanel, BorderLayout.CENTER);
		menuPanel.addExitListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseClicked(MouseEvent e) {
				enableExitInfoPanel();
			}
		});
		this.repaint();
	}
	
	public void addHistoryMovedPanel(HistoryMovedPanel hmp){
		Border b = BorderFactory.createLineBorder(Color.WHITE);
		 String carattere = "TimesRoman";
	     int plain = Font.ITALIC;
	     int dimensioni = 25;
	     Font font = new Font(carattere, plain, dimensioni);
	     this.setForeground(Color.WHITE);
	     
		 TitledBorder tb=BorderFactory.createTitledBorder(b,"Storico mosse",0,0,font);
		  tb.setTitleColor(Color.WHITE);

		this.historyMovedPanel.setLayout(new BorderLayout(0,0));
		this.historyMovedPanel.setBorder(tb);
		this.historyMovedPanel.add(hmp,BorderLayout.CENTER);
		this.hmp=hmp;
		this.repaint();
	
	}
	
	public void addInfoPanel(InfoPanel infoPanel){
		this.infoPanel.setLayout(new BorderLayout());
		this.infoPanel.add(infoPanel,BorderLayout.CENTER);
		this.ip=infoPanel;
		this.repaint();
	}
	
	public void addMoveToHystory(String move){
		 String carattere = "TimesRoman";
	     int plain = Font.ITALIC;
	     int dimensioni = 20;
	     Font font = new Font(carattere, plain, dimensioni);
	     JLabel jLabel = new JLabel(move);
	     jLabel.setForeground(Color.WHITE);
	     jLabel.setFont(font);
		this.hmp.putMove(jLabel);		
		this.repaint();
	}
	
	public void addChatPanel(ChatPanel cp) {
		this.chatPanel.setLayout(new BorderLayout());
		this.cp=cp;
		this.chatPanel.add(this.cp,BorderLayout.CENTER);
		this.repaint();
	}
	
	public void addCheesBoardPanel(ChessBoardPanel cbp) {
		chessBoardPanel.setLayout(new BorderLayout());
		chessBoardPanel.add(cbp,BorderLayout.CENTER);
		this.cbp=cbp;
		
	}
	
	public void disableCheesBoardPanel(){
		cbp.disablePanelListener();
	}
	public void enableCheesBoardPanel(){
		cbp.enablePanelListener();
	}

	public void enableSelecterPiecePanel(utility.Color c){
		selecterPiecePanel = new SelecterPiecePanel();
		selecterPiecePanel.setOpaque(false);
		selecterPiecePanel.addPanel(c);
		selecterPiecePanel.addListener(mlsp);
		secondaryLeftMainLeftPanel.add(selecterPiecePanel,BorderLayout.CENTER);
		
		secondaryLeftMainLeftPanel.revalidate();
		this.repaint();
	}
	
	
	public void enableExitInfoPanel(){
		infoPanel.add(selecterExitInfoPanel);
		selecterExitInfoPanel.addPanel();
		infoPanel.revalidate();
		repaint();
	}
	
	public void addListenerOkExitPanel(MouseListener ml ){
		selecterExitInfoPanel.addListener(ml);
	}
	
	
	
	public void disableSelecterPiecePanel() {
		if(selecterPiecePanel!=null){
			selecterPiecePanel.removePanel();
		}
	}
	
	public void addListenersOfSelectionPiece(MouseListenerSelecterPiece ml){
		mlsp = ml;
	}
	
	public void loading(){
		a=new Animation();
		
		secondaryLeftMainLeftPanel.add(loadingPAnel,BorderLayout.SOUTH);
		loadingPAnel.setLayout(new BorderLayout());
		
		loadingPAnel.add(a, BorderLayout.CENTER);
		 String carattere = "TimesRoman";
	     int plain = Font.ITALIC;
	     int dimensioni = 40;
	     Font font = new Font(carattere, plain, dimensioni);
	     JLabel jLabel = new JLabel("Caricamento...");
	     jLabel.setFont(font);
	     jLabel.setForeground(Color.WHITE);
		loadingPAnel.add(jLabel,BorderLayout.CENTER);
		
	}
	public void loaded(){
		a.stop();
		secondaryLeftMainLeftPanel.removeAll();
		
	}
	
	public void setWhiteTurn(){
		ip.setWhiteTurn();
	}
	
	public void setBlackTurn(){
		ip.setBlackTurn();
	}
	public void setPlayerWait(){
		ip.setPlayerOnWait();
	}
	
	public void setOpponentWait(){
		ip.setOpponentOnWait();
	}
	
	public void setErrorMove(){
		ip.setErrorMove();
	}
	public void removeErrorMove(){
		ip.removeErrorMove();
	}
	
	public void setYouCantMove(){
		ip.setYouCantMove();
	}
	
	public void setPlayerOnWait(){
		ip.setPlayerOnWait();
	}
	
	public void setOpponentOnWait(){
		ip.setOpponentOnWait();
	}
	
	public void setPlayerSelectPiece(utility.Color c){
		ip.setPlayerSelectPiece(c);
	}
	
	public void removeYouCantMove(){
		ip.removeYouCantMove();
	}
	
	public void putPlayerEaten(String path){
		pelp.putPiece(new PieceImage(path));
		revalidate();
	}
	public void putOpponentEaten(String path){
		oelp.putPiece(new PieceImage(path));
		revalidate();
	}
	
	public void addNewMessage(String nick,String message){
		cp.addMessage(nick, message);
	}
	public void setCheck(utility.Color c, Point p) {
		cbp.setCheck(c,p);
		
	}
	public void setMate(String s) {
		cbp.setMate();
		
	}
	
	public int getSpaceFromBottomPlayerEatenListPanel() {
		return spaceFromBottomPlayerEatenListPanel;
	}
	public void setSpaceFromBottomPlayerEatenListPanel(
			int spaceFromBottomPlayerEatenListPanel) {
		this.spaceFromBottomPlayerEatenListPanel = spaceFromBottomPlayerEatenListPanel;
	}
	
	public int getSpaceTopOpponentEatenListPanel() {
		return spaceTopOpponentEatenListPanel;
	}

	public void setSpaceTopOpponentEatenListPanel(int spaceTopOpponentEatenListPanel) {
		this.spaceTopOpponentEatenListPanel = spaceTopOpponentEatenListPanel;
	}

	public void repaintChessboardPanel() {
		cbp.repaint();
		
	}

	public void setMate(Player p) {
		cbp.setMate();
	}

	public void setConnectionLost(String s) {
		cbp.setConnectionLost(s);
	}
	

	
}
